package com.example.service.impl;

import com.example.dto.DeptDTO;
import com.example.entity.Dept;
import com.example.mapper.DeptMapper;
import com.example.service.DeptService;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.time.LocalDateTime;
import java.util.List;

/**
 * 部门服务实现类
 */
@Service
public class DeptServiceImpl implements DeptService {
    
    @Autowired
    private DeptMapper deptMapper;

    /**
     * 查询所有部门
     */
    @Override
    public List<Dept> list() {
        return deptMapper.list();
    }

    /**
     * 根据ID查询部门
     */
    @Override
    public Dept getById(Integer id) {
        return deptMapper.getById(id);
    }

    /**
     * 新增部门
     */
    @Override
    @Transactional
    public void save(DeptDTO deptDTO) {
        Dept dept = new Dept();
        BeanUtils.copyProperties(deptDTO, dept);
        
        // 设置创建和更新时间
        LocalDateTime now = LocalDateTime.now();
        dept.setCreateTime(now);
        dept.setUpdateTime(now);
        
        deptMapper.insert(dept);
    }

    /**
     * 修改部门
     */
    @Override
    @Transactional
    public void update(DeptDTO deptDTO) {
        Dept dept = new Dept();
        BeanUtils.copyProperties(deptDTO, dept);
        
        // 设置更新时间
        dept.setUpdateTime(LocalDateTime.now());
        
        deptMapper.update(dept);
    }

    /**
     * 删除部门
     */
    @Override
    @Transactional
    public void delete(Integer id) {
        deptMapper.deleteById(id);
    }
} 